package com.example.administrator.fastdream.view.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.example.administrator.fastdream.R;
import com.example.administrator.fastdream.date.bean.MyRecordBean;
import com.example.administrator.fastdream.date.bean.OpenRedBean;
import com.example.administrator.fastdream.date.presenter.MyRecordPresenter;
import com.example.administrator.fastdream.date.presenter.OpenRedPresenter;
import com.example.administrator.fastdream.date.utils.CacheUtils;
import com.example.administrator.fastdream.date.utils.DimenUtil;
import com.example.administrator.fastdream.date.utils.MyUtils;
import com.example.administrator.fastdream.view.base.BaseActivity;
import com.example.administrator.fastdream.view.base.BaseDialog;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MyRecordActivity extends BaseActivity implements MyRecordPresenter.MyRecordCallBack, OpenRedPresenter.DataCallBack {

    @BindView(R.id.iv_mine)
    ImageView ivMine;
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_right)
    TextView tvRight;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    private BaseDialog mDialog;
    private BaseDialog.Builder mBuilder;
    private MyRecordPresenter presenter;
    private int page = 1;
    private RecordAdapter recordAdapter;
    private OpenRedPresenter openRedPresenter;
    private int currPosition;
    private String issue;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_record);
        ButterKnife.bind(this);
        ivBack.setVisibility(View.VISIBLE);
        tvTitle.setText("我的记录");
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        presenter = new MyRecordPresenter(this, this);
        presenter.lookmyRecord(page);
        openRedPresenter = new OpenRedPresenter(this, this);
        initListener();
    }

    private void initListener() {
        refreshLayout.setRefreshHeader(new ClassicsHeader(this));
        refreshLayout.setRefreshFooter(new ClassicsFooter(this));
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                page = 1;
                presenter.lookmyRecord(page);
            }
        });
        refreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(RefreshLayout refreshlayout) {
                page++;
                presenter.lookmyRecord(page);
            }
        });
    }

    private void showNumberDialog(String active_number, String issue) {
        mBuilder = new BaseDialog.Builder(this);
        mDialog = mBuilder.setViewId(R.layout.dialog_myrecord_number)
                //设置dialogpadding
                .setPaddingdp(40, 0, 40, 0)
                //设置显示位置
                .setGravity(Gravity.CENTER)
                //设置动画
                .setAnimation(R.style.Alpah_aniamtion)
                //设置dialog的宽高
                .setWidthHeightpx(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT)
                //设置触摸dialog外围是否关闭
                .isOnTouchCanceled(true)
                //设置监听事件
                .builder();
        mDialog.show();
        TextView tv_number = mDialog.getView(R.id.tv_number);
        tv_number.setText(active_number);
        TextView tv_time = mDialog.getView(R.id.tv_time);
        String time = issue.substring(0, 4) + "年" + issue.substring(4, 6) + "月" + issue.substring(6, 8)
                + "日第" + issue.substring(8, issue.length()) + "期";
        tv_time.setText(time);
        mDialog.getView(R.id.iv_cancel).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mDialog.dismiss();
            }
        });
        mDialog.getView(R.id.tv_yes).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mDialog.dismiss();
            }
        });
    }

    @Override
    public void lookmyRecord(MyRecordBean myRecordBean) {
        MyRecordBean.DataEntity data = myRecordBean.getData();
        final List<MyRecordBean.DataEntity.DataEntity2> data1 = data.getData();
        if (recordAdapter == null) {
            recordAdapter = new RecordAdapter(R.layout.item_my_record, data1);
            View view = View.inflate(mContext, R.layout.recycler_header, null);
            ImageView iv_bg = view.findViewById(R.id.iv_bg);
            iv_bg.setBackgroundResource(R.drawable.yuanmenggushi_bg);
            TextView tvCount = view.findViewById(R.id.tv_count);
            tvCount.setText("幸运来了，挡都挡不住！");
            recordAdapter.addHeaderView(view);
            recyclerView.setAdapter(recordAdapter);
        } else {
            if (page == 1) {
                recordAdapter.setNewData(data1);
                refreshLayout.finishRefresh();
            } else {
                if (data1 == null || data1.size() == 0) {
                    Toast.makeText(this, "到底啦~", Toast.LENGTH_SHORT).show();
                    refreshLayout.finishLoadMore();
                    return;
                }
                recordAdapter.addData(data1);
                refreshLayout.finishLoadMore();
            }
        }
        recordAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                if (view.getId() == R.id.tv_right) {
                    TextView textView = (TextView) view;
                    if (textView.getText().toString().equals("去抽红包")) {
                        currPosition = position;
                        issue = data1.get(position).getIssue();
                        showOpenRedBaoDialog(issue);
                    } else {
                        Intent intent = new Intent(mContext, JiangDetailActivity.class);
                        intent.putExtra("issue", data1.get(position).getIssue());
                        startActivity(intent);
                    }
                }
                if (view.getId() == R.id.ll_money) {
                    showNumberDialog(data1.get(position).getActive_number(), data1.get(position).getIssue());
                }
                if (view.getId() == R.id.ll_number) {
                    Intent intent = new Intent(mContext, JiangDetailActivity.class);
                    intent.putExtra("issue", data1.get(position).getIssue());
                    startActivity(intent);
                }
            }
        });
    }

    @Override
    public void date(OpenRedBean dataBean) {
        if (dataBean.getData().getLq_money().getType().equals("1")) {
            showZhongJiangReslutDialog(dataBean.getData().getLq_money().getMoney());
        } else {
            showZhongJiangYuanMengDialog(dataBean.getData().getLq_money().getMoney());
        }
        presenter.lookmyRecord(page);
    }

    private class RecordAdapter extends BaseQuickAdapter<MyRecordBean.DataEntity.DataEntity2, BaseViewHolder> {

        public RecordAdapter(int layoutResId, @Nullable List<MyRecordBean.DataEntity.DataEntity2> data) {
            super(layoutResId, data);
        }

        @Override
        protected void convert(BaseViewHolder helper, MyRecordBean.DataEntity.DataEntity2 item) {
            helper.addOnClickListener(R.id.tv_right);
            helper.addOnClickListener(R.id.ll_money);
            helper.addOnClickListener(R.id.ll_number);
            String issue = item.getIssue();
            String time = issue.substring(0, 4) + "年" + issue.substring(4, 6) + "月" + issue.substring(6, 8)
                    + "日第" + issue.substring(8, issue.length()) + "期";

            helper.setText(R.id.tv_join_count, item.getCanyu_number() + "")
                    .setText(R.id.tv_money, item.getPay_money() + "元")
                    .setText(R.id.tv_luck_number, item.getLucky_number())
                    .setText(R.id.tv_number, item.getActive_number())
                    .setText(R.id.tv_time, time);
            ImageView iv_jiang = helper.getView(R.id.iv_jiang);
            if (TextUtils.isEmpty(item.getWinning_money()) && TextUtils.isEmpty(item.getIs_lq())) {
                helper.setVisible(R.id.tv_right, false);
                iv_jiang.setVisibility(View.VISIBLE);
                iv_jiang.setImageResource(R.drawable.weizhongjiang);
            }
            if (!TextUtils.isEmpty(item.getWinning_type())) {
                if (item.getWinning_type().equals("1")) {
                    helper.setVisible(R.id.tv_right, true);
                    if (item.getIs_lq().equals("0")) {
                        helper.setText(R.id.tv_right, "去抽红包");
                        iv_jiang.setVisibility(View.INVISIBLE);
                    } else if (item.getIs_lq().equals("1")) {
                        helper.setText(R.id.tv_right, "已抽" + item.getWinning_money() + "元");
                        iv_jiang.setVisibility(View.VISIBLE);
                        iv_jiang.setImageResource(R.drawable.xingyunjiang);
                    }
                }else if (item.getWinning_type().equals("2")) {
                    helper.setVisible(R.id.tv_right, true);
                    //                helper.setText(R.id.tv_right, "联系客服");
                    if (item.getIs_lq().equals("0")) {
                        iv_jiang.setVisibility(View.INVISIBLE);
                        helper.setText(R.id.tv_right, "去抽红包");
                    } else if (item.getIs_lq().equals("1")) {
                        iv_jiang.setVisibility(View.VISIBLE);
                        helper.setText(R.id.tv_right, "已抽" + item.getWinning_money() + "元");
                        RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) iv_jiang.getLayoutParams();
                        layoutParams.topMargin = 0;
                        iv_jiang.setLayoutParams(layoutParams);
                        iv_jiang.setImageResource(R.drawable.zhongdajiang);
                    }
                }
            }
        }
    }

    @OnClick(R.id.iv_back)
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
        }
    }

    private void showOpenRedBaoDialog(final String Issue) {
        mBuilder = new BaseDialog.Builder(this);
        int dialog_width = MyUtils.getScreenWidth(this) - DimenUtil.dp2px(this, 40);
        int dialog_height = (int) (dialog_width / 0.87f);
        mDialog = mBuilder.setViewId(R.layout.dialog_openhongbao)
                //设置dialogpadding
                .setPaddingdp(20, 0, 20, 0)
                //设置显示位置
                .setGravity(Gravity.CENTER)
                //设置动画
                .setAnimation(R.style.Alpah_aniamtion)
                //设置dialog的宽高
                .setWidthHeightpx(LinearLayout.LayoutParams.MATCH_PARENT, dialog_height)
                //设置触摸dialog外围是否关闭
                .isOnTouchCanceled(true)
                //设置监听事件
                .builder();
        mDialog.show();
        mDialog.getView(R.id.view_open).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mDialog.dismiss();
                openRedPresenter.data((String) CacheUtils.get("token"), Issue);
            }
        });
        mDialog.getView(R.id.tv_detail).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mDialog.dismiss();
            }
        });
    }

    private void showZhongJiangReslutDialog(String yuan) {
        mBuilder = new BaseDialog.Builder(this);
        int dialog_width = MyUtils.getScreenWidth(this) - DimenUtil.dp2px(this, 40);
        int dialog_height = (int) (dialog_width / 0.87f);
        mDialog = mBuilder.setViewId(R.layout.dialog_zhongjiang_result)
                //设置dialogpadding
                .setPaddingdp(20, 0, 20, 0)
                //设置显示位置
                .setGravity(Gravity.CENTER)
                //设置动画
                .setAnimation(R.style.Alpah_aniamtion)
                //设置dialog的宽高
                .setWidthHeightpx(LinearLayout.LayoutParams.MATCH_PARENT, dialog_height)
                //设置触摸dialog外围是否关闭
                .isOnTouchCanceled(true)
                //设置监听事件
                .builder();
        mDialog.show();
        ((TextView) mDialog.getView(R.id.tv_yuan)).setText(yuan + "元");
        mDialog.getView(R.id.tv_yes).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mDialog.dismiss();
            }
        });
        mDialog.getView(R.id.tv_detail).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mDialog.dismiss();
                Intent intent = new Intent(mContext, JiangDetailActivity.class);
                intent.putExtra("issue",issue);
                startActivity(intent);
            }
        });
    }

    private void showZhongJiangYuanMengDialog(String yuan) {
        mBuilder = new BaseDialog.Builder(this);
        int dialog_width = MyUtils.getScreenWidth(this) - DimenUtil.dp2px(this, 40);
        int dialog_height = (int) (dialog_width / 0.87f);
        mDialog = mBuilder.setViewId(R.layout.dialog_zhongjiang_yuanmeng)
                //设置dialogpadding
                .setPaddingdp(20, 0, 20, 0)
                //设置显示位置
                .setGravity(Gravity.CENTER)
                //设置动画
                .setAnimation(R.style.Alpah_aniamtion)
                //设置dialog的宽高
                .setWidthHeightpx(LinearLayout.LayoutParams.MATCH_PARENT, dialog_height)
                //设置触摸dialog外围是否关闭
                .isOnTouchCanceled(true)
                //设置监听事件
                .builder();
        mDialog.show();
        ((TextView) mDialog.getView(R.id.tv_yuan)).setText(yuan + "元");
        mDialog.getView(R.id.tv_yes).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mDialog.dismiss();
            }
        });
        mDialog.getView(R.id.tv_detail).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mDialog.dismiss();
                startActivity(new Intent(mContext,MyWalletActivity.class));
            }
        });
    }

    private void showMeiZhongJiangDialog() {
        mBuilder = new BaseDialog.Builder(this);
        int dialog_width = MyUtils.getScreenWidth(this) - DimenUtil.dp2px(this, 40);
        int dialog_height = (int) (dialog_width / 0.87f);
        mDialog = mBuilder.setViewId(R.layout.dialog_meizhongjiang)
                //设置dialogpadding
                .setPaddingdp(20, 0, 20, 0)
                //设置显示位置
                .setGravity(Gravity.CENTER)
                //设置动画
                .setAnimation(R.style.Alpah_aniamtion)
                //设置dialog的宽高
                .setWidthHeightpx(LinearLayout.LayoutParams.MATCH_PARENT, dialog_height)
                //设置触摸dialog外围是否关闭
                .isOnTouchCanceled(true)
                //设置监听事件
                .builder();
        mDialog.show();
        mDialog.getView(R.id.view_zhidaole).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                mDialog.dismiss();
            }
        });
        mDialog.getView(R.id.tv_detail).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mDialog.dismiss();
                Intent intent = new Intent(mContext, JiangDetailActivity.class);
                intent.putExtra("issue",issue);
                startActivity(intent);
            }
        });
    }
}
